import axios from "axios";

function randomToken(length = 8) {
  const ku = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  let result = "";
  for (let i = 0; i < length; i++) {
    const randomindex = Math.floor(Math.random() * ku.length);
    result += ku[randomindex];
  }
  return result;
}

const instance = axios.create({
  baseURL: "http://localhost:8888/",
  timeout: 3000,
});
instance.interceptors.request.use(
  (config) => {
    if (!config.headers.token) {
      config.headers.token = localStorage.getItem("token") || randomToken();
    }
    return config;
  },
  (err) => {
    console.log(err.message);
  }
);
instance.interceptors.response.use(
  (res) => {
    console.log(res.data);
  },
  (err) => {
    console.log(err.message);
  }
);
export default instance;
